<?php
namespace Home\Controller;
use Think\Controller;
use Think\AjaxPage;
use Think\Page;
class UserController extends Controller {
	public function _initialize() {
		$token = I('token')?I('token'):'';
		if($token){
			$u = M('users')->where(array('token'=>$token))->field('id,status')->find();	
			if(!$u['id']){
				exit(json_encode(array('code'=>400,'number'=>rand(1,99),'msg'=>'请先登录','jn_msg'=>'最初にログインしてください')));						
			}else if(!$u['status']){
				exit(json_encode(array('code'=>600,'number'=>rand(1,99),'cn'=>'您的账号已被冻结，请联系管理员。','jn'=>'あなたのアカウントは凍結されました。管理者に連絡してください。')));	
			}else{
				$this->user_id = $u['id'];
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'请先登录','jn_msg'=>'最初にログインしてください')));			
		}
		check_goods();
	}
	public function add_goods(){
		$user_id = $this->user_id;
		$mod = M('users');	
		$u = $mod->where(array('id'=>$user_id))->field('type_id,goods_num,surplus_num')->find();
		if($u['type_id']==1){
			$goods_mod = M('goods');
			$data['lang'] = I('lang')?I('lang'):'';
			$data['store_id'] = $user_id;
			$data['title'] = I('title')?I('title'):'';
			if(!$data['title']){
				exit(json_encode(array('code'=>100,'msg'=>'请输入标题','jn_msg'=>'タイトルを入力してください')));		
			}
			$data['resume'] = I('resume')?I('resume'):'';
			if(!$data['resume']){
				exit(json_encode(array('code'=>100,'msg'=>'请输入要求备注','jn_msg'=>'リクエストノートを入力してください')));		
			}
			$time = time();
			if(!$_FILES['thumb']){
				exit(json_encode(array('code'=>100,'msg'=>'请上传主图','jn_msg'=>'メイン画像をアップロードしてください')));		
			}else{
				$data['thumb'] = $this->up_goods_img('thumb');
			}
			$data['price'] = I('price')?I('price'):'';
			if(!$data['price']){
				exit(json_encode(array('code'=>100,'msg'=>'请输入价格','jn_msg'=>'価格を入力してください')));		
			}
			$data['cat_id']	= I('cat_id')?I('cat_id'):'';
			if(!$data['cat_id']){
				exit(json_encode(array('code'=>100,'msg'=>'请选择分类','jn_msg'=>'カテゴリを選択してください')));		
			}
			if(!$u['goods_num']){
				exit(json_encode(array('code'=>100,'msg'=>'发布商品已达到上限','jn_msg'=>'製品が限界に達しました')));	
			}
			
			$data['inventory']	= I('inventory')?I('inventory'):'';
			$data['add_time'] =$time;
			$result = $goods_mod->add($data);
			if($result){
				$img_mod = M('goods_img');
				if($_FILES['img1']){
					$img1 = $this->up_goods_img('img1');
					$img1 && $img_mod->add(array('thumb'=>$img1,'goods_id'=>$result,'add_time'=>time()));
				}
				if($_FILES['img2']){
					$img2 = $this->up_goods_img('img2');
					$img2 && $img_mod->add(array('thumb'=>$img2,'goods_id'=>$result,'add_time'=>time()));
				}
				if($_FILES['img3']){
					$img3 = $this->up_goods_img('img3');
					$img3 && $img_mod->add(array('thumb'=>$img3,'goods_id'=>$result,'add_time'=>time()));
				}
				$mod->where(array('id'=>$user_id))->save(array('goods_num'=>$u['goods_num']-1,'surplus_num'=>$u['surplus_num']+1));
				exit(json_encode(array('code'=>200,'msg'=>'发布成功','jn_msg'=>'正常にリリースされました')));				
			}			
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'暂无权限','jn_msg'=>'許可なし')));		
		}
	}
	public function edit_goods(){
		$user_id = $this->user_id;
		$mod = M('users');	
		$id = I('id')?I('id'):'';
		$type_id = $mod->where(array('id'=>$user_id))->getField('type_id');
		if($type_id==1 && $id){
			$goods_mod = M('goods');
			$data['lang'] = I('lang')?I('lang'):'';
			$data['store_id'] = $user_id;
			$data['title'] = I('title')?I('title'):'';
			if(!$data['title']){
				exit(json_encode(array('code'=>100,'msg'=>'请输入标题','jn_msg'=>'タイトルを入力してください')));		
			}
			$data['resume'] = I('resume')?I('resume'):'';
			if(!$data['resume']){
				exit(json_encode(array('code'=>100,'msg'=>'请输入要求备注','jn_msg'=>'リクエストノートを入力してください')));		
			}
			$time = time();
			if($_FILES['thumb']){
				$data['thumb'] = $this->up_goods_img('thumb');
			}
			
			$data['price'] = I('price')?I('price'):'';
			if(!$data['price']){
				exit(json_encode(array('code'=>100,'msg'=>'请输入价格','jn_msg'=>'価格を入力してください')));		
			}
			$data['cat_id']	= I('cat_id')?I('cat_id'):'';
			if(!$data['cat_id']){
				exit(json_encode(array('code'=>100,'msg'=>'请选择分类','jn_musg'=>'カテゴリを選択してください')));		
			}
			$data['inventory']	= I('inventory')?I('inventory'):'';
			$data['up_time'] =$time;
			$result = $goods_mod->where(array('id'=>$id))->save($data);
			if($result){
				$img_mod = M('goods_img');
				if($_FILES['img1']){
					$img1 = $this->up_goods_img('img1');
					$img1 && $img_mod->add(array('thumb'=>$img1,'goods_id'=>$id,'add_time'=>time()));
				}
				if($_FILES['img2']){
					$img2 = $this->up_goods_img('img2');
					$img2 && $img_mod->add(array('thumb'=>$img2,'goods_id'=>$id,'add_time'=>time()));
				}
				if($_FILES['img3']){
					$img3 = $this->up_goods_img('img3');
					$img3 && $img_mod->add(array('thumb'=>$img3,'goods_id'=>$id,'add_time'=>time()));
				}
				exit(json_encode(array('code'=>200,'msg'=>'发布成功','jn_msg'=>'正常にリリースされました')));				
			}			
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'暂无权限','jn_msg'=>'許可なし')));		
		}
	}
	public function goods_reminder(){
		$user_id = $this->user_id;
		$day = M('config')->where(array('id'=>3))->getField('value');
		$time = time();
		$day_time = $time+intval($day)*86400;
		$num = M('goods')->where("store_id=$user_id and status=1 and audit_time>$time and audit_time<$day_time")->count();
		if($num){
			exit(json_encode(array('code'=>200,'cn'=>'您有'.$num.'个商品即将过期，请尽快联系管理员续费。','jn'=>'有効期限が近づいているアイテムが'.$num.'つあります。できるだけ早く更新のために管理者に連絡してください。')));
		}else{
			exit(json_encode(array('code'=>100)));	
		}
	}
	public function set_shelves(){
		$user_id = $this->user_id;
		$mod = M('users');	
		$id = I('id')?I('id'):'';
		$type_id = $mod->where(array('id'=>$user_id))->getField('type_id');
		if($type_id==1 && $id){
			$time = time();			
			$goods_mod = M('goods');
			$g = $goods_mod->where(array('id'=>$id))->field('audit,audit_time')->find();
			if($g['audit']<1){	
				if($time>$g['audit_time'] && $g['audit_time']>0){
					exit(json_encode(array('code'=>100,'msg'=>'上架周期已到，请联系管理员续费。','jn_msg'=>'保存期間が到来しました。更新のために管理者に連絡してください。')));
				}
				$where['audit'] = 2;
				$result = $goods_mod->where(array('id'=>$id))->save($where);
				if($result){
					goods_log($id,1,'申请上架');
					exit(json_encode(array('code'=>200,'msg'=>'上架成功','jn_msg'=>'成功した棚に')));				
				}	
			}else{
				exit(json_encode(array('code'=>200,'msg'=>'上架失败','jn_msg'=>'棚に着くことができなかった')));		
			}
		}		
	}
	
	public function set_restore(){
		$user_id = $this->user_id;
		$mod = M('goods');	
		$id = I('id')?I('id'):'';
		$tid = I('tid')?I('tid'):'';
		$store_id = $mod->where(array('id'=>$id))->getField('store_id');
		if($store_id==$user_id && $id){
			$time = time();			
			if($tid==1){
				$name = '暂停出售';
				$data['status'] = 0;
			}else{
				$name = '恢复出售';
				$data['status'] = 1;
			}
			$data['up_time'] = $time;
			$result = $mod->where(array('id'=>$id))->save($data);
			if($result){
				goods_log($id,1,$name);
				exit(json_encode(array('code'=>200,'msg'=>'暂停成功','jn_msg'=>'中断されました')));				
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'参数错误','jn_msg'=>'パラメータエラー')));	
		}		
	}
	
	public function up_img($id,$thumb){
		if($thumb){
			$img_mod = M('goods_img');
			$time = time().rand(1000,9999);
			$thumb = str_replace('data:image/jpeg;base64,', '', $thumb);
			$path = ".";
			$output_file = '/Public/upload/goods/'.$time.'.png';
			$path = $path.$output_file;
			file_put_contents($path, base64_decode($thumb));
			$img_mod->add(array('goods_id'=>$id,'thumb'=>$output_file,'add_time'=>time()));
		}		
	}
	public function del_img(){
		$thumb = I('thumb')?I('thumb'):'';
		if($thumb){
			$img_mod = M('goods_img');
			$result = $img_mod->where(array('thumb'=>str_replace(C('WEB_URL'),'',$thumb)))->delete();
			if($result){
				exit(json_encode(array('code'=>200)));				
			}else{
				exit(json_encode(array('code'=>100)));		
			}
		}		
	}
	public function del_goods(){
		$user_id = $this->user_id;
		$mod = M('users');	
		$id = I('id')?I('id'):'';
		$type_id = $mod->where(array('id'=>$user_id))->getField('type_id');
		if($type_id==1 && $id){
			$goods_mod = M('goods');
			$where['is_detele'] = 1;
			$result = $goods_mod->where(array('id'=>$id))->save($where);
			if($result){
				exit(json_encode(array('code'=>200,'msg'=>'删除成功','jn_msg'=>'正常に削除されました')));				
			}		
		}
	}
	public function goods_list(){
		$user_id = $this->user_id;
		$mod = M('goods');	
		$where['is_detele'] = 0;
		$where['store_id'] = $user_id;
		$type_id = I('type_id')?I('type_id'):0;
		$where['audit'] = $type_id;
		$page = I('page')?I('page'):1;
		$row_count = $mod->where($where)->count();
		
		$page_count=4;		
		$total_pages=ceil($row_count/$page_count);		
		$page<1 && $page=1;
		$start_row=($page-1)*$page_count;
		
		$list = $mod->where($where)->order('add_time desc')->field("id,title,price,CONCAT('".C('WEB_URL')."',thumb) as thumb,audit_time,status,audit")->limit($start_row,$page_count)->select();
		foreach((array)$list as $k=>$v){
			$v['audit_time'] && $list[$k]['audit_time'] = date('Y-m-d',$v['audit_time']);
		}
		exit(json_encode(array('code'=>200,'data'=>$list,"nowpage"=>$page,"rollPage"=>$total_pages)));
	}
	
	public function goods_detail(){
		$id = I('id')?I('id'):'';
		$lang = I('lang')?I('lang'):'';
		if($id){
			$mod = M('goods');	
			$detail = $mod->where(array('id'=>$id))->find();
			$cate = M('goods_cat')->where(array('id'=>$detail['cat_id']))->field('name,en_name')->find();
			$detail['cat_name'] = $lang=='cn'?$cate['name']:$cate['en_name'];
			$detail['resume'] = htmlspecialchars_decode($detail['resume']);
			$detail['thumb'] && $detail['thumb'] = C('WEB_URL').$detail['thumb'];
			$detail['goods_img'] = M('goods_img')->where(array('goods_id'=>$id))->field("CONCAT('".C('WEB_URL')."',thumb) as thumb")->select();
			if($detail['store_id']){
				$detail['username'] = M('users')->where(array('id'=>$detail['store_id']))->getField('username');
			}else{
				$detail['username'] = '';				
			}
			exit(json_encode(array('code'=>200,'data'=>$detail)));
		}
	}
	
	public function set_sound(){
		$user_id = $this->user_id;
		$mod = M('users');	
		
		$where['user_id'] = $user_id;
		$result = $mod->where($where)->save(array('sound'=>I('sound')));
		exit(json_encode(array('code'=>200,'data'=>$result)));
	}
	public function set_info(){
		$user_id = $this->user_id;
		$mod = M('users');	
		$where['id'] = $user_id;
		$contents = I('contents')?I('contents'):'';
		$num = I('num')?I('num'):'';
		if($num==26 || $num==67){
			$data['realname'] = $contents;
		}else if($num==27){
			$data['mobile'] = $contents;
		}else if($num==20){
			$data['address'] = $contents;
		}else if($num==23){
			$data['email'] = $contents;
		}else if($num==65){
			$data['company'] = $contents;
		}else if($num==66){
			$data['store_name'] = $contents;
		}else if($num==1){
			$data['country'] = $contents;
		}else if($num==73){
			$data['sex'] = $contents;
		}
		$data['up_time'] = time();
		$result = $mod->where($where)->save($data);
		if($result){
			exit(json_encode(array('code'=>200,'data'=>$result)));
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'修改失败','jn_msg'=>'変更に失敗しました')));			
		}
	}
	function up_goods_img($name){
		$user_id = $this->user_id;
		$mod = M('users');	
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize = 3145728;
		$upload->rootPath = './Public/upload/';
		$upload->savePath = 'goods/';
		$upload->saveName = array('uniqid','');
		$upload->exts     = array('jpg','gif','png','jpeg');
		$upload->autoSub  = false;//子目录
		$upload->subName  = array('Public','Ymd');
		$info   =   $upload->uploadOne($_FILES[$name]);
		if($info){	
			return '/Public/upload/goods/'.$info['savename'];				
		}
	}
	function user_img(){
		$user_id = $this->user_id;
		$mod = M('users');	
		if($_FILES['head_img']['name']){		
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize = 3145728;
			$upload->rootPath = './Public/upload/';
			$upload->savePath = 'user/';
			$upload->saveName = array('uniqid','');
			$upload->exts     = array('jpg','gif','png','jpeg');
			$upload->autoSub  = false;//子目录
			$upload->subName  = array('Public','Ymd');
			$info   =   $upload->uploadOne($_FILES['head_img']);
			if($info){	
				$image = new \Think\Image();
				$image->open($upload->rootPath.$upload->savePath.$info['savename']);
				$data['head_img'] = '/Public/upload/user/'.'s_'.$info['savename'];
				$image->thumb(750,750,\Think\Image::IMAGE_THUMB_CENTER)->save('.'.$data['head_img']);
				$data['up_time'] = time();
				$result = $mod->where(array('id'=>$user_id))->save($data);
				if($result){
					exit(json_encode(array('code'=>200,'data'=>C('WEB_URL').$data['head_img'])));
				}else{
					exit(json_encode(array('code'=>100,'msg'=>'修改失败','jn_msg'=>'変更に失敗しました')));	
				}				
			}else{
				
				exit(json_encode(array('code'=>100,'msg'=>'修改失败','jn_msg'=>'変更に失敗しました','data'=>$upload->getError())));			
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'修改失败','jn_msg'=>'変更に失敗しました')));				
		}
	}
	
	public function store_order(){
		$user_id = $this->user_id;
		$mod = M('order');	
		$where['store_id'] = $user_id;
		$page = I('page')?I('page'):1;
		$row_count = $mod->where($where)->count();
		
        $page_count=10;		
		$total_pages=ceil($row_count/$page_count);		
		$page<1 && $page=1;
		$start_row=($page-1)*$page_count;
		
		$list = $mod->where($where)->order('add_time desc')->limit($start_row,$page_count)->select();
		$goods_mod = M('goods');
		
		foreach((array)$list as $k=>$v){
			$list[$k]['add_time'] = date('Y/m/d H:i',$v['add_time']);
			$list[$k]['goods_list'] = $goods_mod->where(array('id'=>$v['goods_id']))->field("id,title,price,CONCAT('".C('WEB_URL')."',thumb) as thumb")->select();
		}
		exit(json_encode(array('code'=>200,'data'=>$list,'rollPage'=>$total_pages,'nowPage'=>$page)));
	}
	
	public function order_list(){
		$user_id = $this->user_id;
		$mod = M('order');	
		$where['user_id'] = $user_id;
		$page = I('page')?I('page'):1;
		$page_count=10;	
		$count = $mod->where($where)->count();
		$total_pages=ceil($count/$page_count);		
		$page<1 && $page=1;
		$start_row=($page-1)*$page_count;
		$list = $mod->where($where)->order('add_time desc')->limit($start_row,$page_count)->select();
		$goods_mod = M('goods');
		foreach((array)$list as $k=>$v){
			$list[$k]['add_time'] = date('Y/m/d H:i',$v['add_time']);
			$list[$k]['goods_list'] = $goods_mod->where(array('id'=>$v['goods_id']))->field("id,title,price,CONCAT('".C('WEB_URL')."',thumb) as thumb")->select();
		}
		exit(json_encode(array('code'=>200,'data'=>$list,'rollPage'=>$total_pages,'nowPage'=>$page)));
	}
	
	public function order_detail(){
		$user_id = $this->user_id;
		$id = I('id')?I('id'):'';
		$mod = M('order');	
		$detail = $mod->where(array('id'=>$id))->find();
		$detail['add_time'] = date('Y-m-d H:i',$detail['add_time']);
		if($detail['up_time']>0){
			$detail['up_time'] = date('Y-m-d H:i',$detail['up_time']);
		}else{
			$detail['up_time'] = '';
		}
		$detail['goods'] = M('goods')->alias('as a')
			->join('LEFT JOIN '.C('DB_PREFIX').'order as b on a.id=b.goods_id')
			->where(array('a.id'=>$detail['goods_id'],'b.id'=>$id))->field("a.id,a.title,a.price,CONCAT('".C('WEB_URL')."',a.thumb) as thumb,b.goods_number")->select();
		exit(json_encode(array('code'=>200,'data'=>$detail)));
	}
	
	public function order_cancle(){
		$user_id = $this->user_id;
		$id = I('id')?I('id'):'';
		$mod = M('order');	
		$detail = $mod->where(array('id'=>$id))->find();
		if($detail['order_status']<1){
			$data['order_status'] = 3;
			$data['cancle_time'] = time();
			$result = $mod->where(array('id'=>$id))->save($data);
			if($result){
				$log['order_id'] = $id;
				$log['resume'] = '取消订单';
				$log['add_time'] = time();
				$log['status'] = 3;
				M('order_log')->add($log);
				exit(json_encode(array('code'=>200,'data'=>$detail)));	
			}else{
				exit(json_encode(array('code'=>100,'msg'=>'取消失败','jn_msg'=>'キャンセルの失敗')));
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'参数错误','jn_msg'=>'パラメータエラー')));
		}
	}
	
	public function order_send(){
		$user_id = $this->user_id;
		$id = I('id')?I('id'):'';
		$mod = M('order');	
		$detail = $mod->where(array('id'=>$id))->find();
		if($detail['order_status']<1){
			$data['order_status'] = 1;
			$data['send_time'] = time();
			$result = $mod->where(array('id'=>$id))->save($data);
			if($result){
				$log['order_id'] = $id;
				$log['resume'] = '确认发货';
				$log['status'] = 2;
				$log['add_time'] = time();
				M('order_log')->add($log);
				exit(json_encode(array('code'=>200,'msg'=>'发货成功','jn_msg'=>'成功納品')));	
			}else{
				exit(json_encode(array('code'=>100,'msg'=>'发货失败','jn_msg'=>'配信に失敗しました')));
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'参数错误','jn_msg'=>'パラメータエラー')));
		}
	}
	
	public function order_success(){
		$user_id = $this->user_id;
		$id = I('id')?I('id'):'';
		$mod = M('order');	
		$detail = $mod->where(array('id'=>$id))->find();
		if($detail['order_status']==1){
			$data['order_status'] = 2;
			$data['up_time'] = time();
			$result = $mod->where(array('id'=>$id))->save($data);
			if($result){
				$log['order_id'] = $id;
				$log['resume'] = '确认已收货';
				$log['add_time'] = time();
				M('order_log')->add($log);
				exit(json_encode(array('code'=>200)));	
			}else{
				exit(json_encode(array('code'=>100,'msg'=>'确认失败','jn_msg'=>'確認の失敗')));
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'参数错误','jn_msg'=>'パラメータエラー')));
		}
	}
	
    public function index(){
		$user_id = $this->user_id;
		$mod = M('users');
		$user = $mod->where(array('id'=>$user_id))->field('id,username,realname,mobile,company,email,address,head_img')->find();
		exit(json_encode(array('code'=>200,'data'=>$user)));		
	}
	public function addr_list(){
		$user_id = $this->user_id;
		$mod = M('users_addr');
		$list = $mod->where(array('user_id'=>$user_id))->order('address_id desc')->select();
		exit(json_encode(array('code'=>200,'data'=>$list)));	
	}
	public function addr_default(){
		$user_id = $this->user_id;
		$mod = M('users_addr');
		$address_id = I('address_id')?I('address_id'):'';
		if($address_id){
			$mod->where(array('user_id'=>$user_id))->save(array('is_default'=>0));
			$mod->where(array('address_id'=>$address_id))->save(array('is_default'=>1));
			exit(json_encode(array('code'=>200,'data'=>$list)));	
		}
	}
	public function addr_info(){
		$user_id = $this->user_id;
		$mod = M('users_addr');
		$address_id = I('address_id')?I('address_id'):'';
		if($address_id){
			$info = $mod->where(array('address_id'=>$address_id))->find();
			exit(json_encode(array('code'=>200,'data'=>$info)));	
		}
	}
	public function addr(){
		$user_id = $this->user_id;
		$mod = M('users_addr');
		$id = I('address_id')?I('address_id'):'';
		
		$data['consignee'] = I('consignee');
		$data['mobile'] = I('mobile');
		$data['addr'] = I('addr');
		$data['province'] = I('province');
		$data['district'] = I('district');
		$data['city'] = I('city');
		$data['address'] = I('address');
		$data['is_default'] = I('is_default');
		$data['user_id'] = $user_id;
		if($id){
			$data['up_time'] = time();
			$result = $mod->where(array('address_id'=>$id))->save($data);
			if($result){
				if($data['is_default']==1){
					$mod->where(array('user_id'=>$user_id,'address_id'=>array('neq',$id)))->save(array('is_default'=>0));
				}
				exit(json_encode(array('code'=>200,'data'=>$result)));		
			}else{
				exit(json_encode(array('code'=>100,'msg'=>'添加失败','jn_msg'=>'追加に失敗しました')));			
			}
		}else{
			$result = $mod->add($data);
			if($result){
				if($data['is_default']==1){
					$mod->where(array('user_id'=>$user_id,'address_id'=>array('neq',$result)))->save(array('is_default'=>0));
				}
				exit(json_encode(array('code'=>200,'data'=>$result)));		
			}else{
				exit(json_encode(array('code'=>100,'msg'=>'添加失败','jn_msg'=>'追加に失敗しました')));			
			}
		}
	}
}